<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { defaultFormData, formRules } from '../index'
import type { KPIType } from '../type'
import type { CustomDialogInstance } from '@/types'
// ------------------use------------------
// #region
const props = defineProps<{
  title: string
  data: typeof defaultFormData
}>()
const emits = defineEmits<{
  'save-success': []
}>()
// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
const formData = reactive<KPIType>({ ...defaultFormData })
// #endregion
// ------------------computed------
// #region

// #endregion
// ------------------生命周期------------------
// #region

// #endregion
// ------------------内置方法---------------------
// #region
watch(
  () => toValue(props.data),
  (data: typeof defaultFormData) =>
    Object.assign(formData, data),
)
// #endregion
// ------------------方法---------------------
// #region
async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  formRef.value?.resetFields()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    emits('save-success')
  }
  catch (error) {
    done()
  }
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="right"
      :model="formData"
      :rules="formRules"
      inline
    >
      <el-form-item
        label="班组"
        prop="team"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.team"
          maxlength="20"
          placeholder="请输入班组"
        />
      </el-form-item>
      <el-form-item
        label="日期"
        prop="time"
        class="width-fill"
      >
        <el-date-picker
          v-model="formData.time"
          type="date"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item
        label="考核制"
        prop="assessment"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.assessment"
          maxlength="20"
          placeholder="请输入考核制"
        />
      </el-form-item>
      <el-form-item
        label="当前值(%)"
        prop="currentValue"
        class="width-fill"
      >
        <el-input-number
          v-model.number="formData.currentValue"
          controls-position="right"
          :min="0"
          :max="100"
          placeholder="请输入名称"
        />
      </el-form-item>
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
